//
//  BaseTable.m
//  SelfDriving
//
//  Created by apple on 15/10/22.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "BaseTable.h"
#import "ImageService.h"
@implementation BaseTable{
    int pageindex;
}
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    
    //    [self setupRefresh];
}
    
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupRefresh];
    }
    return self;
}
    
-(void)Refresh{
    [self headerRereshing];
    [self.header beginRefreshing];
}
    /**
     *  集成刷新控件
     */
- (void)setupRefresh {
    if (self.header) {
        [self headerRereshing];
        [self.header beginRefreshing];
        
    }else{
        self.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self headerRereshing];
        }];
        
        [self.header beginRefreshing];
    }
}
    
#pragma mark 开始进入刷新状态
- (void)headerRereshing {
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        pageindex = 1;
        [self.bdelegate initData:pageindex];
        [self reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.header endRefreshing];
    });
}
- (void)footerRereshing {
    // 2.2秒后刷新表格UI 多线程
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //xhw2018-1-18加
        if (_xhwIndex) {
            pageindex = _xhwIndex;
        }
        // 刷新表格
        pageindex ++;
        [self.bdelegate initData:pageindex];
        [self reloadData];
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.footer endRefreshing];
    });
}
    //无参数提示
-(UIView *)notData{
    _noDataLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.origin.x, self.frame.size.height/2-15, self.frame.size.width, 30)];
    _noDataLabel.text = @"暂无数据";
    _noDataLabel.textColor = UIColorFromRGB(0x666666);
    _noDataLabel.textAlignment = NSTextAlignmentCenter;
    _noDataLabel.font = [UIFont systemFontOfSize:15];
    return _noDataLabel;
}
    //处理返回
-(NSMutableArray *)result:(NSMutableArray *)rsdata olddata:(NSMutableArray *)olddata index:(int)index{
    //    self.hidden = YES;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (index == 1) {
        [olddata removeAllObjects];
    }
    if ([rsdata count] > 0) {
        //        self.hidden = NO;
        [olddata addObjectsFromArray:rsdata];
    }
    if (index == 1 && [rsdata count] <= 0) {
        [self.footer removeFromSuperview];
        [olddata removeAllObjects];
    }
    
    if ([rsdata count] >= 20) {
        self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self footerRereshing];
        }];
    }else if([rsdata count]==0){
        [self.footer removeFromSuperview];
    }
    return olddata;
}

#pragma 动态-基础style
-(int)Basics:(UITableViewCell*)cell dtmodel:(DTModel *)model bgview:(UIView *)bgview{
    bgview.backgroundColor = [UIColor whiteColor];
    bgview.layer.cornerRadius = 4;
    
    
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(40,35, 20, 20)];
    img.image = [UIImage imageNamed:@"vip"];
    if ([model.memberlevel intValue]==0) {
        img.hidden = YES;
    }else{
        img.hidden = NO;
    }
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(65, 0, 180, 30)];
    //这里如果发布的快拍有标题，昵称处显示标题，否则显示昵称。(xhw9月21改)
//    if (![model.title  isEqual: @""]) {
//        name.text = model.title;
//    }else {
    name.text = model.nickname;
//    }

    name.font = [UIFont systemFontOfSize:15];
    [bgview addSubview:name];
    
    UILabel *logintime = [[UILabel alloc]initWithFrame:CGRectMake(SCRE_WIDTH-100, 0, 90, 30)];
    logintime.text = [ValidateHelper timeStringWith:[model.createtime doubleValue]multiLine:NO];
    logintime.font = [UIFont systemFontOfSize:12];
    logintime.textColor = [UIColor grayColor];
    logintime.textAlignment = NSTextAlignmentRight;
    [bgview addSubview:logintime];
    
    [cell.contentView addSubview:bgview];
    
    UIImageView *headimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
    [headimg sd_setImageWithURL:[NSURL URLWithString:model.headsmall]
               placeholderImage:[UIImage imageNamed:@"mrtx"]];
    headimg.layer.cornerRadius = headimg.frame.size.width/2;
    headimg.clipsToBounds = YES;
    [cell.contentView addSubview:headimg];
    [cell.contentView addSubview:img];
    
    return 45;
}
-(int)Attachment:(UITableViewCell *)cell dtmodel:(DTModel *)model bgview:(UIView *)bgview{
    NSMutableDictionary *dic = [model.attachment lastObject];
    NSString *url;
    CGFloat rh = SCRE_WIDTH;
    if(!dic){
        url = model.img;
    }else{
        url = [dic objectForKey:@"path"];
    }
    NSString  *ptr;
    if([url trim].length!=0){
        ptr = [url substringFromIndex:url.length-3];
        
    }
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 45, SCRE_WIDTH, SCRE_WIDTH)];
    if ([ptr isEqualToString:@"png"]||[ptr isEqualToString:@"jpg"]) {
        UIImageView *img = [[UIImageView alloc]init];
        
        rh = [ValidateHelper setimgeSize:url img:img];
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.clipsToBounds = YES;
#pragma mark -- 动态高度设置
        CGRect im = img.frame;
        //限制高度
        //        im.size.height = img.image.size.height>SCRE_WIDTH?SCRE_WIDTH:img.image.size.height;
        im.size.height = SCRE_WIDTH;
        
        CGFloat proportion =  SCREEN_WIDTH / img.image.size.width;
        
        
        //不限制高度
        //        im.size.height = img.image.size.height * proportion;
        
        
        im.size.width = SCRE_WIDTH;
        //        img.frame = view.frame;
        img.frame = im;
        
        rh = im.size.height;
        
        //        img.contentMode = UIViewContentModeScaleAspectFill;
        
        [view addSubview:img];
    }else{
        NSString *url = [dic objectForKey:@"path"];
        NSString *video = [dic objectForKey:@"video"];
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, SCRE_WIDTH)];
        if (![ValidateHelper isHttpHead:video]) {
            video = [NSString stringWithFormat:@"%@%@",img_Url,video];
        }
        if (![ValidateHelper isHttpHead:url]) {
            url = [NSString stringWithFormat:@"%@%@",img_Url,url];
        }
        //        MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:url]];
        //        UIImage  *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
        //        img.image = thumbnail;
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.clipsToBounds = YES;
        [img sd_setImageWithURL:[NSURL URLWithString:video]
               placeholderImage:[UIImage imageNamed:@"notimgs"]];
        //        img.image = [ImageService resizeImage:img.image toSize:CGSizeMake(SCRE_WIDTH-20, 220)];
        
        
        [view addSubview:img];
    }
    
    [bgview addSubview:view];
    return rh;
}
    
    
    @end
